﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ALDIOrganizer
{
    public partial class SetDayStatus : Form
    {
        #region Start-up
        public SetDayStatus()
        {
            InitializeComponent();
            SetRadioButtonsAndCheckboxes();
        }

        public SetDayStatus(string pFormCaption, string pGroupBoxCaption, bool[] pInitialValues)
            : this()
        {
            this.Text = pFormCaption;
            gbMain.Text = pGroupBoxCaption;            
            SetValues(pInitialValues);
        }

        private void formSpecialDayStatuses_Load(object sender, EventArgs e)
        {
            
        }
        #endregion

        public Weekday[] GetDays
        {
            get
            {
                var _result = new List<Weekday>();
                var _day = 0;
                foreach (var _bit in GetResult)
                {
                    if(_bit)
                        _result.Add((Weekday)_day);
                    _day++;
                }
                return _result.ToArray();
            }
        }

        IEnumerable<bool> GetResult
        {
            get
            {
                if (cbMultiple.Checked)
                    return (from checkbox in cbWeekdays
                            select checkbox.Checked).ToArray();
                return (from _radiobutton in rbWeekdays
                        select _radiobutton.Checked).ToArray();
            }
        }

        void SetValues(bool[] pValues)
        {
            if (pValues == null)
                return;
            if (pValues.Length != 7)
                return;
            var moreThanOne = ((pValues.Where(value => value)).Count()) > 1;
            
            cbMultiple.Checked = moreThanOne;

            var index = 0;
            if (moreThanOne)
            {
                foreach (var combobox in cbWeekdays)
                {
                    cbWeekdays[index].Checked = pValues[index++];
                }
            }
            else
            {
                foreach (var radiobutton in rbWeekdays)
                {
                    rbWeekdays[index].Checked = pValues[index++];
                }
            }            
        }

        #region checkboxes / radiobuttons
        CheckBox[] cbWeekdays;
        RadioButton[] rbWeekdays;        

        void SetRadioButtonsAndCheckboxes()
        {
            rbWeekdays = new RadioButton[7];
            rbWeekdays[0] = rbDay1;
            rbWeekdays[1] = rbDay2;
            rbWeekdays[2] = rbDay3;
            rbWeekdays[3] = rbDay4;
            rbWeekdays[4] = rbDay5;
            rbWeekdays[5] = rbDay6;
            rbWeekdays[6] = rbDay7;
                        
            cbWeekdays = new CheckBox[7];
            for (int day = 0; day < 7; day++)
            {
                rbWeekdays[day].Text = Time.ShortDayName(day);
                
                cbWeekdays[day] = new CheckBox {Text = Time.ShortDayName(day), TabIndex = day};
                rbWeekdays[day].Tag = day;
                cbWeekdays[day].Tag = day;
                cbWeekdays[day].Enabled = false;
                cbWeekdays[day].Visible = false;
                cbWeekdays[day].AutoSize = true;
                cbWeekdays[day].Location = rbWeekdays[day].Location;
                cbWeekdays[day].Top++;
                
                this.gbMain.Controls.Add(cbWeekdays[day]);
                                
                rbWeekdays[day].Tag = day;                
            }
        }
        #endregion

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (cbMultiple.Checked)
                foreach (var _checkbox in cbWeekdays)
                {
                    _checkbox.Checked = false;
                }
            else
                foreach (var _radiobutton in rbWeekdays)
                {
                    _radiobutton.Checked = false;
                }
        }

        void FromRadiobuttonsToCheckboxes()
        {
            var _index = 0;            
            foreach (var _radiobutton in rbWeekdays)            
                cbWeekdays[_index++].Checked = _radiobutton.Checked;                        
        }

        void FromCheckboxesToRadiobuttons()
        {
            var _index = 0;
            var _checked = false;
            foreach (var _checkbox in cbWeekdays)
            {
                if ((_checkbox.Checked) & (!_checked))
                {
                    rbWeekdays[_index].Checked = _checkbox.Checked;
                    _checked = true;
                }
                else
                    rbWeekdays[_index].Checked = false;
                _index++;
            }
        }

        private void cbMultiple_CheckedChanged(object sender, EventArgs e)
        {
            gbMain.SuspendLayout();            
            var multiple = cbMultiple.Checked;
            for (var day = 0; day < 7; day++)
            {
                rbWeekdays[day].Enabled = !multiple;
                rbWeekdays[day].Visible = !multiple;
                cbWeekdays[day].Enabled = multiple;
                cbWeekdays[day].Visible = multiple;                
            }
            if (multiple)
                FromRadiobuttonsToCheckboxes();
            else
                FromCheckboxesToRadiobuttons();
            gbMain.ResumeLayout();
        }

        
    }
}
